import * as dayjs from "dayjs"

// 将数组对象，按照对象中的某个属性的属性值进行分组，如：[{updated: '2021-01-01'}, {updated: '2021-01-02'}]，按照updated属性值月份分组
// 示例：groupByAttr([{updated: '2021-01-01', name: '张三'}, {updated: '2021-01-02', name: '李四'}], 'updated')
export const groupByMonth = (arr: any[]) => {
    if (!arr.length) return []
    const obj: any = {}
    arr.forEach(item => {
        const month = dayjs(item.time).format('YYYY-MM')
        if (!obj[month]) {
            obj[month] = []
        }
        obj[month].push(item)
    })
    return obj
}

// 获取图片路径
export function getImageUrl(path: string): string {
    if (path && /^(http|https|data):\/\//.test(path)) {
        return new URL(path, import.meta.url).href;
    }
    return new URL('../../assets/img/' + path, import.meta.url).href
}

// 是否是移动端
export function isMobile(): boolean {
    return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
}

// 是否是微信浏览器
export function isWeixin(): boolean {
    return /MicroMessenger/i.test(navigator.userAgent);
}

// 是否是QQ浏览器
export function isQQ(): boolean {
    return /QQ/i.test(navigator.userAgent);
}

// 是否是IOS
export function isIOS(): boolean {
    return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);
}

// 是否是Android
export function isAndroid(): boolean {
    return /(Android)/i.test(navigator.userAgent);
}

// 是否是PC端
export function isPC(): boolean {
    return !isMobile();
}

// 是否是Mac
export function isMac(): boolean {
    return /macintosh|mac os x/i.test(navigator.userAgent);
}

// 是否是Windows
export function isWindows(): boolean {
    return /windows|win32/i.test(navigator.userAgent);
}

// 浏览器类型
// 是否是Chrome
export function isChrome(): boolean {
    return /chrome/i.test(navigator.userAgent);
}

// 是否是IE
export function isIE(): boolean {
    return /msie/i.test(navigator.userAgent);
}
